﻿using UnityEngine;
using System.Collections;

public enum FacePhase
{
    MovingIn,
    MovingOut,
    Shaking
};

public class FaceMovementScript : MonoBehaviour 
{
    private GameObject persistentManager;
    private EyeMovementScript eyeMovementScript;
    private Transform middle;
    private FacePhase phase;
    private Renderer faceTexture;
    private GameObject teeth;

    public Texture closedMouth;
    public Texture openMouth;
    public Texture dead;

    private float speed = 8.0f;
    private float t = 0;
    public int intensity = 0;
    float xCycle;
    float yCycle;

	void Start () 
    {
        persistentManager = GameObject.FindGameObjectWithTag("Manager");
        if(persistentManager.GetComponent<PersistentManagerScript>().GetState() == GameState.game)
            middle = GameObject.Find("Middle").transform;
        teeth = transform.FindChild("Teeth").gameObject;
        teeth.SetActive(false);
        faceTexture = gameObject.renderer;
        faceTexture.material.SetTexture("_MainTex", closedMouth);
        GrayFaceKappa();
        xCycle = Random.Range(1, 4);
        yCycle = Random.Range(1, 4);
        eyeMovementScript = GetComponent<EyeMovementScript>();

        if (persistentManager.GetComponent<PersistentManagerScript>().GetState() != GameState.game)
        {
            BrightFace();
        }
    }
	
	void Update () 
    {
        if (persistentManager.GetComponent<PersistentManagerScript>().GetState() == GameState.game)
        {
            switch (phase)
            {
                case FacePhase.MovingIn:
                    transform.position = Vector3.MoveTowards(transform.position, middle.position, speed * Time.deltaTime);
                    if (transform.position.Equals(middle.position))
                    {
                        phase = FacePhase.Shaking;
                        BrightFace();
                        teeth.SetActive(true);
                        faceTexture.material.SetTexture("_MainTex", openMouth);
                        if (eyeMovementScript != null)
                            GetComponent<EyeMovementScript>().SendMessage("LookAt", LookDirection.mouse, SendMessageOptions.DontRequireReceiver);

                    }
                    break;

                case FacePhase.MovingOut:
                    if(middle == null)
                        middle = GameObject.Find("Middle").transform;
                    transform.position = Vector3.MoveTowards(transform.position, middle.position + new Vector3(-12, 0, 0), speed * Time.deltaTime);
                    break;

                case FacePhase.Shaking:
                    ShakeHead();
                    break;
            }
        }
	}

    private void ShakeHead()
    {

        t += (Time.deltaTime/10) * intensity;
        float xShaking = Mathf.Sin((t * xCycle % Mathf.PI * 2)) * (1 / xCycle);
        float yShaking = Mathf.Sin((t * yCycle % Mathf.PI * 2)) * (1 / yCycle);
        transform.position = middle.position + new Vector3(xShaking, yShaking);
    }

    public bool FaceCompleted()
    {
        phase = FacePhase.MovingOut;
        GrayFaceKappa();
        Destroy(gameObject, 16.0f / speed);
        teeth.SetActive(false);
        faceTexture.material.SetTexture("_MainTex", closedMouth);        
        return false;
    }

    public bool GetClickable()
    {
        return phase == FacePhase.Shaking;
    }

    private void GrayFaceKappa()
    {
        renderer.material.color = new Color(0.5f, 0.5f, 0.5f);
        Renderer[] teeth = transform.Find("Teeth").GetComponentsInChildren<Renderer>();
        foreach (Renderer rend in teeth)
        {
            rend.material.color = new Color(0.5f, 0.5f, 0.5f);
        }
    }

    private void BrightFace()
    {
        renderer.material.color = new Color(1.0f, 1.0f, 1.0f);
        Renderer[] teeth = transform.Find("Teeth").GetComponentsInChildren<Renderer>();
        foreach (Renderer rend in teeth)
        {
            rend.material.color = new Color(1.0f, 1.0f, 1.0f);
        }
    }

    public bool IsEglibleForOperation()
    {
        return phase == FacePhase.Shaking;
    }

}
